package admin

import (
	"context"
	"fmt"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/text/gstr"
	"github.com/gogf/gf/v2/util/gconv"
	"miaoyu/internal/dao"
	"miaoyu/internal/enums/roleenum"
	"miaoyu/internal/model"
	"miaoyu/internal/model/admin"
	"miaoyu/internal/model/entity"
	"miaoyu/internal/service"
	function "miaoyu/internal/utility"
	"time"
)

type sLogin struct {
}

func init() {
	service.RegisterLogin(NewLogin())
}

func NewLogin() *sLogin {
	return &sLogin{}
}

func (s *sLogin) CheckUserPassword(ctx context.Context, input admin.UserLoginInput) (resUser entity.MyUser, resErr error) {
	/*
		//对密码进行rsa解密
		originData, rsaErr := function.RsaDecrypt([]byte(input.Password))
		fmt.Println("ordata:", originData, rsaErr)
		if rsaErr != nil {
			resErr = rsaErr
			return
		}
	*/
	originData := input.Password

	//fmt.Println("checkpassword", originData)
	var shaWord string = function.MakeAdminPassWord(gconv.String(originData))
	fmt.Println("加密密码：", shaWord)

	resUser, resErr = dao.MyUser.GetUserByUserNamePassword(ctx, input.UserName, shaWord)

	if resErr != nil {

		return
	}
	return
}

// DoOauthLogin 服务端登陆用，生成auth token 专用
func (s *sLogin) DoOauthLogin(ctx context.Context, user entity.MyUser) (string, time.Time, error) {
	payloadMap := s.getPayloadFormat(user)
	token, expire, err := service.OauthAuth().TokenGenerator(payloadMap)

	if err == nil {
		superId, _ := g.Cfg().Get(ctx, "superadmin.id")
		//获取用户信息，储存session
		contextUser := model.ContextUser{}
		contextUser.UserName = user.UserName
		contextUser.Nickname = user.RealName
		contextUser.Id = user.Id
		if user.Id == gconv.Int64(superId) {
			contextUser.IsAdmin = true
			//##################//
			//contextUser.CmsRoleIds = "5"
		} else {
			contextUser.IsAdmin = false
			//

			roleList, _ := service.Role().GetRoleListByUserId(ctx, user.Id, roleenum.AbilityRole)
			cmsList, _ := service.Role().GetRoleListByUserId(ctx, user.Id, roleenum.CmsDataRole)
			contextUser.RoleIds = gstr.JoinAny(roleList, ",")
			contextUser.CmsRoleIds = gstr.JoinAny(cmsList, ",")

		}

		service.Session().SetUser(ctx, &contextUser)
	}

	return token, expire, err
}
func (s *sLogin) DoLogin(ctx context.Context, user entity.MyUser) (string, time.Time, error) {
	payloadMap := s.getPayloadFormat(user)
	token, expire, err := service.Auth().TokenGenerator(payloadMap)

	if err == nil {
		superId, _ := g.Cfg().Get(ctx, "superadmin.id")
		//获取用户信息，储存session
		contextUser := model.ContextUser{}
		contextUser.UserName = user.UserName
		contextUser.Nickname = user.RealName
		contextUser.Id = user.Id
		if user.Id == gconv.Int64(superId) {
			contextUser.IsAdmin = true
			//##################//
			//contextUser.CmsRoleIds = "5"
		} else {
			contextUser.IsAdmin = false
			//

			roleList, _ := service.Role().GetRoleListByUserId(ctx, user.Id, roleenum.AbilityRole)
			cmsList, _ := service.Role().GetRoleListByUserId(ctx, user.Id, roleenum.CmsDataRole)
			contextUser.RoleIds = gstr.JoinAny(roleList, ",")
			contextUser.CmsRoleIds = gstr.JoinAny(cmsList, ",")

		}

		service.Session().SetUser(ctx, &contextUser)
	}

	return token, expire, err
}
func (s *sLogin) getPayloadFormat(data entity.MyUser) (res model.ContextUser) {

	res.Id = data.Id
	res.UserName = data.UserName
	return
}
func (s *sLogin) DoLogout(ctx context.Context) {
	service.Session().RemoveUser(ctx)
	service.Auth().LogoutHandler(ctx)
}
